<template>
  <div id="spaceManagePage">
    <a-flex justify="space-between">
      <h2>空间管理</h2>
      <a-space>
        <a-button type="primary" href="/add_space" target="_blank">+ 创建空间</a-button>
        <a-button type="primary" ghost href="/space_analyze?queryPublic=1" target="_blank"
          >分析公共图库</a-button
        >
        <a-button type="primary" ghost href="/space_analyze?queryAll=1" target="_blank"
          >分析全部空间</a-button
        >
      </a-space>
    </a-flex>
    <div style="margin-bottom: 16px" />
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="空间名称">
        <a-input v-model:value="searchParams.spaceName" placeholder="请输入空间名称" allow-clear />
      </a-form-item>
      <a-form-item name="spaceLevel" label="空间级别">
        <a-select
          v-model:value="searchParams.spaceLevel"
          style="min-width: 180px"
          placeholder="请选择空间级别"
          :options="SPACE_LEVEL_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="空间类别" name="spaceType">
        <a-select
          v-model:value="searchParams.spaceType"
          :options="SPACE_TYPE_OPTIONS"
          placeholder="请输入空间类别"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="用户 id">
        <a-input v-model:value="searchParams.userId" placeholder="请输入用户 id" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px" />
    <!-- 空间管理卡片列表 -->
    <div class="space-management-list">
      <div 
        v-for="record in dataList" 
        :key="record.id" 
        class="space-card"
      >
        <!-- 空间图标区域 -->
        <div class="space-icon">
          <div class="icon-container">
            <div class="space-icon-symbol">
              {{ record.spaceType === 0 ? '🏢' : '👤' }}
            </div>
            <div class="space-level-badge">
              <a-tag :color="record.spaceLevel === 0 ? 'green' : record.spaceLevel === 1 ? 'blue' : 'orange'">
                {{ SPACE_LEVEL_MAP[record.spaceLevel] }}
              </a-tag>
            </div>
          </div>
          <div class="space-overlay">
            <div class="space-id">ID: {{ record.id }}</div>
            <div class="space-type">
              <a-tag color="blue">{{ SPACE_TYPE_MAP[record.spaceType] }}</a-tag>
            </div>
          </div>
        </div>
        
        <!-- 空间信息区域 -->
        <div class="space-info">
          <!-- 基本信息 -->
          <div class="info-section">
            <h3 class="space-name" :title="record.spaceName">{{ record.spaceName || '未命名空间' }}</h3>
            <p class="space-intro" :title="record.spaceIntroduction">{{ record.spaceIntroduction || '暂无简介' }}</p>
          </div>
          
          <!-- 详细信息 -->
          <div class="info-details">
            <!-- 使用情况 -->
            <div class="info-row">
              <div class="info-item">
                <span class="info-label">存储:</span>
                <span class="info-value">{{ formatSize(record.totalSize) }} / {{ formatSize(record.maxSize) }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">数量:</span>
                <span class="info-value">{{ record.totalCount }} / {{ record.maxCount }}</span>
              </div>
            </div>
            
            <div class="info-row">
              <div class="info-item">
                <span class="info-label">用户ID:</span>
                <span class="info-value">{{ record.userId }}</span>
              </div>
            </div>
            
            <!-- 时间信息 -->
            <div class="info-row">
              <div class="info-item">
                <span class="info-label">创建时间:</span>
                <span class="info-value">{{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm') }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">编辑时间:</span>
                <span class="info-value">{{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm') }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 操作按钮区域 -->
        <div class="space-actions">
          <a-space>
            <a-button 
              type="primary"
              size="small"
              :href="`/space_analyze?spaceId=${record.id}`" 
              target="_blank"
            >
              分析
            </a-button>
            <a-button 
              type="default" 
              size="small"
              :href="`/add_space?id=${record.id}`" 
              target="_blank"
            >
              编辑
            </a-button>
            <a-button 
              danger 
              size="small"
              @click="doDelete(record.id)"
            >
              删除
            </a-button>
          </a-space>
        </div>
      </div>
    </div>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <a-pagination
        v-model:current="searchParams.current"
        v-model:pageSize="searchParams.pageSize"
        :total="total"
        :show-size-changer="true"
        :show-quick-jumper="true"
        :show-total="(total, range) => `共 ${total} 条记录，第 ${range[0]}-${range[1]} 条`"
        @change="onPageChange"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteSpaceUsingPost, listSpaceByPageUsingPost } from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import {
  SPACE_LEVEL_MAP,
  SPACE_LEVEL_OPTIONS,
  SPACE_TYPE_MAP,
  SPACE_TYPE_OPTIONS,
} from '../../constants/space.ts'
import { formatSize } from '../../utils'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '空间名称',
    dataIndex: 'spaceName',
  },
  {
    title: '空间级别',
    dataIndex: 'spaceLevel',
  },
  {
    title: '空间类别',
    dataIndex: 'spaceType',
  },
  {
    title: '使用情况',
    dataIndex: 'spaceUseInfo',
  },
  {
    title: '用户 id',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 定义数据
const dataList = ref<API.Space[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.SpaceQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 获取数据
const fetchData = async () => {
  const res = await listSpaceByPageUsingPost({
    ...searchParams,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})

// 分页变化处理
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteSpaceUsingPost({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}
</script>

<style scoped>
#spaceManagePage {
  padding: 2rem;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  margin: 1rem;
}

/* 页面标题样式 */
#spaceManagePage h2 {
  color: var(--text-primary);
  font-weight: 600;
}

/* 黑色模式下的页面样式 */
[data-theme="dark"] #spaceManagePage {
  background: rgba(31, 41, 55, 0.9);
  border: 1px solid rgba(75, 85, 99, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] #spaceManagePage h2 {
  color: #e5e7eb;
}

/* 黑色模式下的表单标签 */
[data-theme="dark"] #spaceManagePage :deep(.ant-form-item-label > label) {
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框 */
[data-theme="dark"] #spaceManagePage :deep(.ant-input),
[data-theme="dark"] #spaceManagePage :deep(.ant-select-selector) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框悬停状态 */
[data-theme="dark"] #spaceManagePage :deep(.ant-input:hover),
[data-theme="dark"] #spaceManagePage :deep(.ant-select:hover .ant-select-selector) {
  border-color: rgba(14, 165, 233, 0.6) !important;
}

/* 黑色模式下的输入框聚焦状态 */
[data-theme="dark"] #spaceManagePage :deep(.ant-input:focus),
[data-theme="dark"] #spaceManagePage :deep(.ant-input-focused),
[data-theme="dark"] #spaceManagePage :deep(.ant-select-focused .ant-select-selector) {
  border-color: #0ea5e9 !important;
  box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.2) !important;
}

/* 黑色模式下的占位符文字 */
[data-theme="dark"] #spaceManagePage :deep(.ant-input::placeholder),
[data-theme="dark"] #spaceManagePage :deep(.ant-select-selection-placeholder) {
  color: #9ca3af !important;
}

/* 黑色模式下的选择项 */
[data-theme="dark"] #spaceManagePage :deep(.ant-select-selection-item) {
  color: #e5e7eb !important;
  background: rgba(14, 165, 233, 0.2) !important;
  border-color: rgba(14, 165, 233, 0.3) !important;
}

/* 黑色模式下的表格 */
[data-theme="dark"] #spaceManagePage :deep(.ant-table) {
  background: rgba(31, 41, 55, 0.8) !important;
  color: #e5e7eb;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-table-thead > tr > th) {
  background: rgba(17, 24, 39, 0.8) !important;
  color: #f3f4f6 !important;
  border-color: rgba(75, 85, 99, 0.3) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-table-tbody > tr > td) {
  background: rgba(31, 41, 55, 0.6) !important;
  color: #e5e7eb !important;
  border-color: rgba(75, 85, 99, 0.3) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(55, 65, 81, 0.8) !important;
}

/* 黑色模式下的分页组件 */
[data-theme="dark"] #spaceManagePage :deep(.ant-pagination) {
  color: #e5e7eb;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-item) {
  background: rgba(55, 65, 81, 0.8);
  border-color: rgba(75, 85, 99, 0.5);
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-item a) {
  color: #e5e7eb;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-item:hover) {
  border-color: rgba(14, 165, 233, 0.6);
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-item-active) {
  background: rgba(14, 165, 233, 0.2);
  border-color: #0ea5e9;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-item-active a) {
  color: #0ea5e9;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-prev),
[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-next) {
  color: #e5e7eb;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-prev:hover),
[data-theme="dark"] #spaceManagePage :deep(.ant-pagination .ant-pagination-next:hover) {
  color: #0ea5e9;
}

/* 黑色模式下的按钮 */
[data-theme="dark"] #spaceManagePage :deep(.ant-btn:not(.ant-btn-primary)) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-btn:not(.ant-btn-primary):hover) {
  background: rgba(75, 85, 99, 0.8) !important;
  border-color: rgba(14, 165, 233, 0.6) !important;
  color: #f3f4f6 !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-btn-dangerous) {
  background: rgba(239, 68, 68, 0.2) !important;
  border-color: rgba(239, 68, 68, 0.5) !important;
  color: #fca5a5 !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-btn-dangerous:hover) {
  background: rgba(239, 68, 68, 0.3) !important;
  border-color: rgba(239, 68, 68, 0.7) !important;
  color: #f87171 !important;
}

/* 空间管理卡片式布局样式 */
.space-management-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(550px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.space-card {
  display: flex;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  min-height: 150px;
}

.space-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.15);
}

/* 黑色模式下的空间卡片 */
[data-theme="dark"] .space-card {
  background: rgba(31, 41, 55, 0.95);
  border: 1px solid rgba(75, 85, 99, 0.3);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] .space-card:hover {
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

/* 空间图标区域 */
.space-icon {
  position: relative;
  flex-shrink: 0;
  width: 130px;
  height: 150px;
  overflow: hidden;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(147, 51, 234, 0.1));
  display: flex;
  align-items: center;
  justify-content: center;
}

[data-theme="dark"] .space-icon {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.2), rgba(147, 51, 234, 0.2));
}

.icon-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.space-icon-symbol {
  font-size: 3rem;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}

.space-level-badge .ant-tag {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  border-radius: 12px;
  padding: 2px 8px;
}

.space-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  color: white;
  padding: 0.5rem;
  font-size: 0.75rem;
  line-height: 1.2;
}

.space-id {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.space-type .ant-tag {
  margin: 0;
  font-size: 0.7rem;
}

/* 空间信息区域 */
.space-info {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.info-section {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  padding-bottom: 0.75rem;
}

[data-theme="dark"] .info-section {
  border-bottom-color: rgba(255, 255, 255, 0.1);
}

.space-name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.25rem 0;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

[data-theme="dark"] .space-name {
  color: #e5e7eb;
}

.space-intro {
  font-size: 0.875rem;
  color: #6b7280;
  margin: 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

[data-theme="dark"] .space-intro {
  color: #9ca3af;
}

/* 详细信息 */
.info-details {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
}

.info-row {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.info-item.full-width {
  flex: 1 1 100%;
}

.info-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: #6b7280;
  white-space: nowrap;
  flex-shrink: 0;
}

[data-theme="dark"] .info-label {
  color: #9ca3af;
}

.info-value {
  font-size: 0.875rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.4;
}

[data-theme="dark"] .info-value {
  color: #d1d5db;
}

/* 操作按钮区域 */
.space-actions {
  display: flex;
  align-items: flex-end;
  padding: 1rem 1rem 1rem 0;
}

.space-actions .ant-space {
  flex-direction: column;
  gap: 0.4rem !important;
}

.space-actions .ant-btn {
  width: 72px;
  font-size: 0.75rem;
  padding: 4px 8px;
  height: 28px;
}

/* 分页容器 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 1.5rem 0;
  background: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

[data-theme="dark"] .pagination-container {
  background: rgba(31, 41, 55, 0.8);
  border: 1px solid rgba(75, 85, 99, 0.3);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .space-management-list {
    grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
  }
}

@media (max-width: 768px) {
  .space-management-list {
    grid-template-columns: 1fr;
  }
  
  .space-card {
    flex-direction: column;
    min-height: auto;
  }
  
  .space-icon {
    width: 100%;
    height: 120px;
  }
  
  .space-actions {
    padding: 1rem;
  }
  
  .space-actions .ant-space {
    flex-direction: row;
    width: 100%;
  }
  
  .space-actions .ant-btn {
    width: auto;
    flex: 1;
  }
}

/* 黑色模式下的标签 */
[data-theme="dark"] #spaceManagePage :deep(.ant-tag) {
  background: rgba(55, 65, 81, 0.8) !important;
  color: #e5e7eb !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-tag-green) {
  background: rgba(34, 197, 94, 0.2) !important;
  color: #4ade80 !important;
  border-color: rgba(34, 197, 94, 0.3) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-tag-blue) {
  background: rgba(59, 130, 246, 0.2) !important;
  color: #60a5fa !important;
  border-color: rgba(59, 130, 246, 0.3) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-tag-orange) {
  background: rgba(249, 115, 22, 0.2) !important;
  color: #fb923c !important;
  border-color: rgba(249, 115, 22, 0.3) !important;
}

[data-theme="dark"] #spaceManagePage :deep(.ant-tag-red) {
  background: rgba(239, 68, 68, 0.2) !important;
  color: #f87171 !important;
  border-color: rgba(239, 68, 68, 0.3) !important;
}
</style>
